<template>
  <div class="login-page">
    <van-nav-bar safe-area-inset-top title="面经注册" />

    <van-form @submit="onSubmit">
      <van-field
        v-model="username"
        name="username"
        label="用户名"
        placeholder="用户名"
        :rules="[
          { required: true, message: '用户名不能为空哦' },
          { pattern: /^\w{3,}/, message: '用户名不能少于三位数哦！！！' },
        ]"
      />
      <van-field
        v-model="password"
        type="password"
        name="password"
        label="密码"
        placeholder="密码"
        :rules="[
        { required: true, message: '密码不能为空哦' },
        { pattern: /^\w{6,}/, message: '密码是不能少于6位数哦！！！' },
        ]"
      />
      <div style="margin: 16px">
        <van-button square block type="primary" native-type="submit">注册</van-button>
      </div>
      <router-link class="link" to="/login">有账号，请登录 </router-link>
    </van-form>
  </div>
</template>

<script>
import { register } from '@/api/user'
export default {
  name: 'login-page',
  data () {
    return {
      username: '',
      password: ''
    }
  },
  methods: {
    /* 提交表单事件 触发 */
    async onSubmit (value) {
      /* 发起axios 请求 */
      const res = await register(value)
      /* 请求成功执行下面代码 */
      console.log(res)
      this.$toast('注册成功了哦~')
      /* 注册成功跳转到登录页面 */
      this.$router.push('/login')
    }
  }
}
</script>

<style lang="less" scoped>
.link {
  color: #069;
  font-size: 12px;
  padding-right: 20px;
  float: right;
}
</style>
